{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a2e63377",
   "metadata": {},
   "source": [
    "---------\n",
    "* 时间：2022-10-25       \n",
    "\n",
    "----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76d807d3",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "\n",
    "> 1. while循环： while T入额-->无限循环       \n",
    ">> 1.指定次数,while True 可以是新for循环的指定次数，增加一个count计数器即可。     \n",
    ">> 2.while True一般与 用户输入 一起使用，让让用户可以重复输入，知道某些条件成立结束输入（购物车）    \n",
    "> 2.购物车时间（作业）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea555ee7",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "> 1.元组   \n",
    "> 2.集合    \n",
    "> 3.字典 \n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5545220",
   "metadata": {},
   "source": [
    "## 1.元组（tuple)\n",
    "\n",
    "> 1.元组的基本内容（形态、定义）    \n",
    "> 2.元组的性质      \n",
    ">> 1.有序性(indexing, sliceing)      \n",
    ">> 2.<font color=\"red\">**不可变性**</font>，元组没有增、删、改      \n",
    "> 3.元组的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "308c5e8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2100001, 2100002, 2100003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网一_学号 = (2100001,2100002,2100003)\n",
    "print(网一_学号,type(网一_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cf2c0390",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2100001, 2100002, 2100003) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "网二_学号 = 2100001,2100002,2100003\n",
    "print(网二_学号,type(网二_学号))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2fdff349",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2100001"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dd033680",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2100002, 2100003)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_学号[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "67cb1a31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=25, tm_hour=15, tm_min=6, tm_sec=30, tm_wday=1, tm_yday=298, tm_isdst=0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 元组的实际案例\n",
    "import time \n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "eaa212a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4], [3, 4, 5, 6]) <class 'tuple'> 2047717018496\n"
     ]
    }
   ],
   "source": [
    "tuple_text = ([1,2,3,4],[3,4,5,6])\n",
    "print(tuple_text,type(tuple_text),id(tuple_text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2eb11c73",
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple_text[0].append(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "03b05fa9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "([1, 2, 3, 4, 100], [3, 4, 5, 6]) <class 'tuple'> 2047717018496\n",
      "[1, 2, 3, 4, 100] 2047716982464\n"
     ]
    }
   ],
   "source": [
    "print(tuple_text,type(tuple_text),id(tuple_text))\n",
    "print(tuple_text[0],id(tuple_text[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "63797745",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一般情况下，元组里面存放的是    常量（不可变量），何为常量：数字，字符串\n",
    "#                     不存放的是 如，列表，字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6785150c",
   "metadata": {},
   "source": [
    "## 2.集合（sets)\n",
    "> 1.集合的基本概念    \n",
    ">> 1.性质： 没有重复项   \n",
    ">> 2.性质： 无序性\n",
    "\n",
    "> 2.集合的一般方法    \n",
    ">> 1.集合的运算：&    \n",
    ">> 2. 集合的一般方法：intersection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "37e60cab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd']\n",
      "{'a', 'd', 'c', 'b'}\n"
     ]
    }
   ],
   "source": [
    "basket_lists = ['a','b','c','d']\n",
    "basket_sets = {'a','b','c','d'}\n",
    "print(basket_lists)\n",
    "print(basket_sets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "34c3b09b",
   "metadata": {},
   "outputs": [],
   "source": [
    "danceclub = {101,122,393,494,121,123}\n",
    "sportclub = {111,121,122,233,242,234}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e5b9143",
   "metadata": {},
   "source": [
    "* 集合的 与运算，交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "15c49c7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{121, 122}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "danceclub & sportclub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "cf6a6069",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{121, 122}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "danceclub.intersection(sportclub)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2deeddc1",
   "metadata": {},
   "source": [
    "* 集合的 或运算，并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "564ae278",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{101, 111, 121, 122, 123, 233, 234, 242, 393, 494}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "danceclub | sportclub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3d23e7fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(danceclub | sportclub)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1562b570",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{101, 111, 121, 122, 123, 233, 234, 242, 393, 494}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "danceclub.union(sportclub)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ffc70dc",
   "metadata": {},
   "source": [
    "## 3.字典（dict）\n",
    "\n",
    "\n",
    "> 1.字典的基本概念：   \n",
    " >> 1.字典的性质：    \n",
    ">>> 1.无序性     \n",
    ">>> 2.keys是不可变的      \n",
    ">>> 3.keys是唯一的\n",
    "\n",
    " >> 2.字典的形态   \n",
    "\n",
    "> 2.字典的一般方法    \n",
    " >> 1.字典的创建       \n",
    " >> 2.字典的函数和方法\n",
    "  >>> 1.增加        \n",
    "  >>> 2.删除        \n",
    "  >>> 3.修改\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97d287b3",
   "metadata": {},
   "source": [
    "## 3.1 字典的性质和概念"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "51683825",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新一班 = {\n",
    "    21001:\"vv\",\n",
    "    21002:\"cc\",\n",
    "    21003:\"eve\",\n",
    "    21004:\"lili\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9ab3943d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{21001: 'vv', 21002: 'cc', 21003: 'eve', 21004: 'lili'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(网新一班,type(网新一班))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "86c33087",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 因为学号是唯一的，可以作为字典的key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "1a0e17e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{21001: 'vv', 21002: 'cc', 21003: 'eve', 21004: 'lili'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "网二 = {\n",
    "    21001:\"vv\",\n",
    "    21002:\"cc\",\n",
    "    21003:\"eve\",\n",
    "    21004:\"lili\"\n",
    "}\n",
    "print(网二,type(网二))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "8a1690cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'eve'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典的取值，通过keys 进行字典的取值，不像列表、元组和字符串通过index取值\n",
    "网二[21003]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "c7af5310",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二 = {\n",
    "    21001:[\"vv\",\"nyc\",\"girl\",20,\"chat report\"],\n",
    "    21002:\"cc\",\n",
    "    21003:\"eve\",\n",
    "    21004:\"lili\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "501f8a1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['vv', 'nyc', 'girl', 20, 'chat report']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二[21001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9ee357b",
   "metadata": {},
   "source": [
    "## 3.2 字典的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90802029",
   "metadata": {},
   "source": [
    "* 1.按照字典的形式直接创建（key:value）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a147af12",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "网二 = {\n",
    "    21001:[\"vv\",\"nyc\",\"girl\",20,\"chat report\"],\n",
    "    21002:\"cc\",\n",
    "    21003:\"eve\",\n",
    "    21004:\"lili\"\n",
    "}\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2128684c",
   "metadata": {},
   "source": [
    "* 2.使用dict()方法创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fdfe9273",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新四班 = dict(\n",
    "    stu1800001=\"李一峰\",\n",
    "    stu1800002=\"Jack\",\n",
    "    stu1800003=\"Marry\",\n",
    "    stu1800004=\"周杰伦\",\n",
    "\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b28c9c10",
   "metadata": {},
   "source": [
    "* 3.使用字典增加的方式创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "57763257",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用dict()方法创建字典\n",
    "网新四班 = dict(\n",
    "    stu1800001=\"李一峰\",\n",
    "    stu1800002=\"Jack\",\n",
    "    stu1800003=\"Marry\",\n",
    "    stu1800004=\"周杰伦\",\n",
    "\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c8d2e7b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新四班"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd140478",
   "metadata": {},
   "source": [
    "* 4. 使用字典增加的方式创建字典 : dict[key] = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f44e1dd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新一班 = {}\n",
    "网新一班[\"stu1800001\"] = '李一峰'\n",
    "网新一班[\"stu1800002\"] = 'Jack'\n",
    "网新一班[\"stu1800003\"] = 'Marry'\n",
    "网新一班[\"stu1800004\"] = '周杰伦'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e9de1547",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6f8322da",
   "metadata": {},
   "outputs": [],
   "source": [
    "网新一班_keys = [\"stu1800001\",\"stu1800002\",\"stu1800003\",\"stu1800004\"]\n",
    "网新一班_values = ['李一峰','Jack','Marry','周杰伦']\n",
    "网新一班 = {}\n",
    "for i in range(4):\n",
    "    网新一班[网新一班_keys[i]] = 网新一班_values[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fed9613b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1800001': '李一峰',\n",
       " 'stu1800002': 'Jack',\n",
       " 'stu1800003': 'Marry',\n",
       " 'stu1800004': '周杰伦'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网新一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "933704f2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
